/**
 * Created by way on 16/9/17.
 */

const $ = require('../lib/kdom');
import cfg from '../config';
import {get, post, getView} from '../util/api';
import {dateStr} from '../util/tool';

// let _view = require('raw-loader!./create.html'); // eslint-disable-line
let _view = '';

/* global txStartTm */
/* global txEndTm */
/* global btnCreate */
/* global btnList */
/* global spTime */
/* global spMaxPrice */

let _user = {};
let _startTm = null;
let _endTm = null;
let _no = '';

export default class {
  path = '/bid/create$';
  hash = '/bid/create';
  title = '创建拍卖';

  constructor(opt) {
    opt = opt || {};
    this.path = opt.path || this.path;
    this.hash = opt.hash || this.hash;
    this.title = opt.title || this.title;
  }

  // 输出视图
  view(cb) {
    if (!_view) {
      // 静态资源浏览器有缓存,增加时标,强制刷新!
      getView(`${cfg.bid.view.host}${cfg.bid.view.create}?t=${(+new Date())}`, '', (rs) => {
        _view = rs;
        cb(null, _view);
      });
    } else
      cb(null, _view);
  }

  // 在已经加载的视图上操作
  bind(dv, params) {
    _user = params || {};
    // alert(JSON.stringify(_user));
    bindBtnCreate();
    btnList.ontouchstart = () => {
      $.router.go('/bid/list', _user);
    };
  }
}

function bindBtnCreate() {
  // 提交表单
  // btnSubmit.onclick = function () {
  btnCreate.ontouchstart = () => {
    create();
  };
}

function create() {
  if (!txStartTm.value || !txEndTm.value) {
    alert('请输入拍卖开始时间、结束时间！');
    return;
  }

  const val = `pid=${_user.pid}&start=${txStartTm.value}&end=${txEndTm.value}`;
  // alert(`create ${val}`);
  post(`${cfg.bid.api.host}${cfg.bid.api.create}`, val, (err, rs) => {
    // alert(`bid:${rs}`);
    if (err) {
      alert('非常抱歉，网络故障，请稍候重试或联系客服!');
    } else if (rs) {
      const r = JSON.parse(rs);
      if (r.rc === 200) {
        alert('恭喜您，拍卖创建成功！');
      } else
        alert('非常抱歉，拍卖创建失败！');
    }
  });
}
